package com.beilang.hotline.login.page;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.codelookup.model.CodeModel;
import com.beilang.base.core.page.BasePage;
import com.beilang.base.exception.CheckException;
import com.beilang.base.property.AppDefaultProperty;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.util.DateUtil;
import com.beilang.hotline.login.UserAccessTO;
import com.beilang.hotline.user.manager.UserMgr;
import com.beilang.hotline.user.model.UserModel;
import flex.messaging.FlexContext;
import flex.messaging.FlexSession;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("unchecked")
public class LoginPage extends BasePage {
    private UserMgr userMgr;

    private CodeLookupMgr codeLookupMgr;

    public void setUserMgr(UserMgr userMgr) {
        this.userMgr = userMgr;
    }

    public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
        this.codeLookupMgr = codeLookupMgr;
    }

    public UserModel login(UserModel userModel) throws CheckException {
        UserModel user = userMgr.getUser(userModel.getUserId());
        if (user == null) {
            throw new CheckException(AppDefaultProperty.getProperty("error.user.does.not.exist"));
        }
        if (!user.getUserPassword().equals(userModel.getUserPassword())) {
            throw new CheckException(AppDefaultProperty.getProperty("error.user.password.wrong"));
        }
        FlexSession session = FlexContext.getFlexSession();
        session.setAttribute("user", user);
        UserAccessTO accessTO=new UserAccessTO(user.getUserRole());
        List accessList = user.getAccessSystemList();
        if(!"A".equalsIgnoreCase(user.getUserRole())){
            for (Object obj : accessList) {
                String accessSystem = (String) obj;
                if ("U".equalsIgnoreCase(accessSystem)) {
                    accessTO.setUserInd(true);
                } else if ("S".equalsIgnoreCase(accessSystem)) {
                    accessTO.setSysInd(true);
                } else if ("M".equalsIgnoreCase(accessSystem)) {
                    accessTO.setModuleInd(true);
                } else if ("E".equalsIgnoreCase(accessSystem)) {
                    accessTO.setEmailInd(true);
                } else if ("I".equalsIgnoreCase(accessSystem)) {
                    accessTO.setIssueInd(true);
                } else if ("C".equalsIgnoreCase(accessSystem)) {
                    accessTO.setCompanyInd(true);
                } else if ("P".equalsIgnoreCase(accessSystem)) {
                    accessTO.setPersonInd(true);
                } else if ("R".equalsIgnoreCase(accessSystem)) {
                    accessTO.setExportInd(true);
                } else if ("HE".equalsIgnoreCase(accessSystem)) {
                    accessTO.setHistoryExportInd(true);
                }else if ("HD".equalsIgnoreCase(accessSystem)) {
                    accessTO.setHistoryDownloadInd(true);
                }else if ("T".equalsIgnoreCase(accessSystem)) {
                    accessTO.setTitleInd(true);
                }else if ("TM".equalsIgnoreCase(accessSystem)) {
                    accessTO.setTemplateInd(true);
                }else if ("DP".equalsIgnoreCase(accessSystem)) {
                    accessTO.setDeptInd(true);
                }
            }
        }
        user.setUserAccessTO(accessTO);
        return user;
    }

    public void isLoginUser() throws CheckException {
        if (FlexContext.getFlexSession().getAttribute("user") == null)
            throw new CheckException("please login in first");
    }

    public String getMenu() {
        UserModel user = this.getLoginUser();
        UserAccessTO accessTO=user.getUserAccessTO();
        List child1 = new ArrayList();
        List child2 = new ArrayList();
        child2.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "H"));
        child2.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "K"));
        if(accessTO.isUserInd()){
            child1.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "U"));
        }
        if(accessTO.isSysInd()){
            child1.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "S"));
        }
        if(accessTO.isModuleInd()){
            child1.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "M"));
        }
        if(accessTO.isEmailInd()){
            child1.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "E"));
        }
        if(accessTO.isIssueInd()){
            child1.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "I"));
        }
        if(accessTO.isTitleInd()){
            child1.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "T"));
        }
        if(accessTO.isTemplateInd()){
            child1.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "TM"));
        }
        if(accessTO.isDeptInd()){
            child1.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "DP"));
        }
        if(accessTO.isCompanyInd()){
            child2.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "C"));
        }
        if(accessTO.isPersonInd()){
            child2.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "P"));
        }
        if(accessTO.isExportInd()){
            child2.add(codeLookupMgr.getCode(CodeConstant.CODE_ACCESS_FUNCTION_LIST, "R"));
        }

        StringBuffer menu = new StringBuffer("");
        if (child1.size() > 0) {
            menu.append("<menuitem label=\"配置管理\">");
            for (Object aChild1 : child1) {
                CodeModel code = (CodeModel) aChild1;
                menu.append("<menuitem label=\"").append(code.getCodeDesc()).append("\" value=\"").append(code.getCodeId()).append("\"/>");
            }
            menu.append("</menuitem>");
        }
        if (child2.size() > 0) {
            menu.append("<menuitem label=\"事务管理\">");
            for (Object aChild2 : child2) {
                CodeModel code = (CodeModel) aChild2;
                menu.append("<menuitem label=\"").append(code.getCodeDesc()).append("\" value=\"").append(code.getCodeId()).append("\"/>");
            }
            menu.append("</menuitem>");
        }
        menu.append("<menuitem label=\"系统\">").append("<menuitem label=\"修改密码\" value=\"CP\"/>").append("<menuitem label=\"退出系统\" value=\"logout\"/>").append("</menuitem>");
        return menu.toString();
    }

    public boolean changePassword(UserModel userModel) throws CheckException {
        UserModel user = userMgr.getUser(getLoginUser().getUserId());
	    if(user==null){
	    	throw new CheckException(AppDefaultProperty.getProperty("error.user.does.not.exist"));
	    }
	    if(!user.getUserPassword().equals(userModel.getUserPassword())){
	    	throw new CheckException(AppDefaultProperty.getProperty("error.user.password.wrong"));
	    }
	    if(!userModel.getUserNewPassword().equals(userModel.getConfirmPassword())){
	    	throw new CheckException(AppDefaultProperty.getProperty("error.user.password.different"));
	    }
	    user.setUserPassword(userModel.getUserNewPassword());
	    user.setUpdateId(user.getUserId());
		user.setUpdateTime(DateUtil.getDate());
		userMgr.saveUser(user);
		return true;
    }

    public UserAccessTO getUserAccessPermission(){
        return this.getLoginUser().getUserAccessTO();
    }

    public void exitSystem(){
        FlexContext.getFlexSession().removeAttribute("user");        
    }

}